package org.rslai.autotest.core.driver.ie;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * ie 配置信息处理
 * @author rslai
 */
public class InternetExplorerProfile {
	private Properties pros;
	public void setPros(Properties pros) {
		this.pros = pros;
	}

	/**
	 * 返回需要设置的字段名的内容
	 * @param key 字段名
	 * @return 内容
	 */
	private String getCapabilityName(String key) {
		try {
			String clazzName = "org.openqa.selenium.ie.InternetExplorerDriver";
			Class<?> clazz = Class.forName(clazzName);
			Field field = clazz.getDeclaredField(key);
			return (String)field.get(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 设置 IE 配置信息
	 * @param capability DesiredCapabilities
	 * @param key
	 * @param value
	 */
	private void setPreference(DesiredCapabilities capability, String key, String value) {
		if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) {
			capability.setCapability(this.getCapabilityName(key), Boolean.valueOf(value).booleanValue());
		} else {
			capability.setCapability(this.getCapabilityName(key), value);
		}
	}

	/**
	 * 设置 IE 信息
	 * @param capability DesiredCapabilities
	 */
	public void setInternetExplorerProfile(DesiredCapabilities capability) {
		Iterator<Entry<Object, Object>> it = pros.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Object, Object> entry = it.next();
			this.setPreference(capability, (String) entry.getKey(), (String) entry.getValue());
		}
	}

}
